<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-breadcrumb>
              <a-breadcrumb-item href="/dashboard/analysis">
                <a-icon type="home" />
                <span>首页</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <a-icon type="user" />
                <span>供应商信息管理</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <span>
                <template v-if="this.id !== ''"> <a-icon type="edit" />供应商信息编辑</template>
                  <template v-else> <a-icon type="folder-add" />供应商信息添加</template>
                </span>
              </a-breadcrumb-item>
            </a-breadcrumb>
          </a-col>
          <a-col :span="24">&nbsp;</a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-page-search-wrapper">
      <a-form :form="form" @submit="handleSubmit">
        <a-row :gutter="24">
          <a-col :span="16">
            <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-button-group>
                <a-button type="primary" @click="goBack"> <a-icon type="left" />返回 </a-button>
                <a-button type="primary" html-type="submit"> 保存 <a-icon type="right" /></a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
          <a-col :span="8" :style="{ textAlign: 'right' }">
            <a-form-item>
              <a-button-group>
                <a-button type="primary" @click="toAudit">审批 <a-icon type="right" /></a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item style="display: none;width:0px;" :labelCol="labelCol" :wrapperCol="wrapperCol"  label="id">
              <a-input v-decorator="['id']" />
            </a-form-item>
            <a-form-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['code']" :disabled="disabledOne" placeholder="请输入供应商编码"></a-input>
            </a-form-item>
            <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name']" :disabled="disabledOne" placeholder="请输入供应商名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contacts']" :disabled="disabledOne" placeholder="请输入联系人"></a-input>
            </a-form-item>
            <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone']" :disabled="disabledOne" placeholder="请输入联系电话"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['address']" :disabled="disabledOne" placeholder="请输入地址"></a-input>
            </a-form-item>
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['email']" :disabled="disabledOne" placeholder="请输入邮箱"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
    </div>

    <!-- table区域-begin -->
    <div>
      订单记录
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"></a-table>
    </div>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@lone/lone-vue/src/utils/mixin'
  import { SingleListMixin } from '@lone/lone-vue/src/mixins/SingleListMixin'
  import JDate from '@lone/lone-vue/src/components/jeecg/JDate'
  import { postAction, putAction, deleteAction, getAction } from '@lone/lone-vue/src/api/manage'
  import Vue from 'vue'
  import ACol from 'ant-design-vue/es/grid/Col'

  export default {
    name: 'MmsSupplierDetailsList',
    mixins:[SingleListMixin, mixinDevice],
    components: {
      ACol,
      JDate
    },
    props:{
      //判断是编辑还是新增
      status:{
        type:String,
        required:false
      },
      //判断是编辑Id
      id:{
        type:String,
        required:false
      },
    },
    watch:{
      status: {
        handler(newVal, oldVal) {
          this.loadFormData()
        },
        immediate: true
      }
    },
    data () {
      return {
        description: 'mms_wz_purchase_quality_details管理页面',
        top: 10,
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        // 表头
        disabledAll: false,
        disabledOne: false,
        editingKey: '',
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'订单编号',
            align:"center",
            dataIndex: 'materialCode'
          },
          {
            title:'订单名称',
            align:"center",
            dataIndex: 'materialName'
          },
          {
            title:'合同编号',
            align:"center",
            dataIndex: 'specs'
          },
          {
            title:'合同日期',
            align:"center",
            dataIndex: 'dd'
          },
          {
            title:'订单金额',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title:'录入人',
            align:"center",
            dataIndex: 'currentArrivalNum'
          },
          {
            title:'录入日期',
            align:"center",
            dataIndex: 'model'
          }
        ],
        formData:{},
        dataSource: [],
        url: {
          list: "/cg/mmsWzPurchaseOrder/list",
          delete: "/base/mmsSupplier/delete",
          deleteBatch: "/base/mmsSupplier/deleteBatch",
          exportXlsUrl: "/base/mmsSupplier/exportXls",
          importExcelUrl: "/base/mmsSupplier/importExcel",
        },
        dictOptions:{},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      loadFormData() {
        let that = this;
        this.dataSource = [];
        if(this.id !== '@ADD@'){
          let paramsObject = {id: this.id};
          getAction('/base/mmsSupplier/queryById', paramsObject).then((res) => {
            if (res.success) {
              let result = res.result;
              if (result) {
                for(let p in result) {
                  let field = {};
                  field[p] = result[p];
                  this.form.setFieldsValue(field);
                }

                /*let paramsObject = {materialCode: result['code'], warehouseCode:  that['warehouseCode']};
               getAction('/ck/mmsWzWarehouseStock/getStockDetailBatch', paramsObject).then((res) => {
                 if (res.success) {
                   let result = res.result;
                   if (result) {
                     for(let p in result) {
                       let field = {};
                       field[p] = result[p];
                     }
                   }
                 }
               });*/

              }
            }
          });
        }
        that.disabledOne = true;
      },
      handleDelete: function (id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            if (that.device == that.LoneDevice_M) {
              that.loadData(1);
            } else {
              that.loadData();
            }

          } else {
            that.$message.warning(res.message);
          }
        });
      },
      toAudit(){
        let that = this, id = that.formData.id;
        putAction('/base/mmsSupplier/audit', {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            let result = res.result;
            if (result) {
              that.redirect(id);
            }
          }else {
            that.$message.warning(res.message);
          }
        })
      },
      handleSubmit(e){
        let that = this, paramsObject = {}, id = that.id;
        e.preventDefault();
        this.form.validateFields((err, values) => {
          if (!err) {
            for(let param in values){
              this.$set(paramsObject, param, values[param]);
            }
            let action = postAction, url = '/base/mmsSupplier/add';
            if(paramsObject.hasOwnProperty('id') && paramsObject['id']){
              url = '/base/mmsSupplier/edit';
              action = putAction;
            }
            action(url, paramsObject).then((res) => {
              if (res.success) {
                this.$message.success(res.message);
                // this.$emit('ok');
                let result = res.result;
                if (result) {
                  that.redirect(result['id'])
                }
              } else {
                this.$message.warning(res.message);
                that.redirect(id);
              }
            }).finally(() => {
            })
          }
        });
      },
      redirect(id){
        let that = this;
        that.$emit("prevStep");
        that.$emit("nextStep",{id:id,status: ''})
      },
      goBack(){
        this.$emit('prevStep')
      },
      initDictConfig(){
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

</style>